package com.zjs.serice.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjs.dao.SellerDao;
import com.zjs.dao.UserDao;
import com.zjs.pojo.Seller;
import com.zjs.pojo.User;
import com.zjs.serice.SellerService;
import com.zjs.util.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import vo.AddUserVo;
import vo.UpdatePassword;
import vo.UserInfoVo;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SellerServiceimpl implements SellerService {
    @Autowired
    private SellerDao dao;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;


    @Override
    public ResultStatus login(Seller seller) {
        String name = seller.getName();
        QueryWrapper<Seller> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Seller::getName,name);
        wrapper.last("limit 1");
        Seller seller1 = dao.selectOne(wrapper);
        if (seller1==null){
            return ResultStatus.FTLED(300,"用户名不存在");
        }
        String upassword = seller1.getPassword();
        if (!upassword.equals(seller.getPassword())){
            return ResultStatus.FTLED(300,"密码不存在");
        }
        Boolean aBoolean = redisTemplate.hasKey(seller1.getSellerId() + "");
        if (aBoolean){
            redisTemplate.delete(seller1.getSellerId() + "");
        }
        ValueOperations<String, String> value = redisTemplate.opsForValue();
        HashMap<String, Object> payload = new HashMap<>();
        payload.put("id",seller1.getSellerId());
        payload.put("uname",seller1.getName());
        String token = JWTUtil.createToken(payload, "123456".getBytes());

        String key = "pc:"+seller1.getSellerId();
        value.set(key,token,30, TimeUnit.MINUTES);
        value.set(token,token,30, TimeUnit.MINUTES);

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(seller1,userInfoVo);
        userInfoVo.setToken(token);

        return  ResultStatus.SUCCESS(userInfoVo);
    }


    @Override
    public ResultStatus updetepassword(UpdatePassword updatepassword) {
        Seller user = dao.selectById(updatepassword.getUserId());
        if(user == null) {
            return ResultStatus.FTLED(404, "用户不存在");
        }
        if(!user.getPassword().equals(updatepassword.getOldPass())) {
            return ResultStatus.FTLED(403, "密码不正确");
        }
        user.setPassword(updatepassword.getNewPass());
        dao.updateById(user);

        return ResultStatus.SUCCESS();
    }



    @Override
    public ResultStatus addUser(AddUserVo addUserVo) {

        // 判断验证码
        String key = addUserVo.getMobile();
        String redisCode = (String) redisTemplate.opsForValue().get(key);
        if (redisCode == null) {
            return ResultStatus.FTLED(410, "验证码过期");
        }
        if (!redisCode.equals(addUserVo.getVerifyCode())) {
            return ResultStatus.FTLED(410, "验证码错误");
        }

        // 判断用户名是否存在(判断手机号)
        LambdaQueryWrapper<Seller> query = new QueryWrapper<Seller>().lambda()
                .eq(Seller::getName, addUserVo.getUserName());
        query.or().eq(Seller::getMobile, addUserVo.getMobile());
        query.last("limit 1");

        Seller user = dao.selectOne(query);
        if (user != null) {
            return ResultStatus.FTLED(410, "用户已存在");
        }
        // 开始注册
        Seller seller = new Seller();
        seller.setName(addUserVo.getUserName());
        seller.setPassword(addUserVo.getUserPass());
        seller.setMobile(addUserVo.getMobile());


        dao.insert(seller);

        return ResultStatus.SUCCESS();
    }



}
